package org.csu.mypetstore.service;

import org.csu.mypetstore.domain.*;
import org.csu.mypetstore.persistence.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class CatalogService {

    @Autowired
    private CategoryMapper categoryMapper;

    @Autowired
    private ProductMapper productMapper;

    @Autowired
    private CartMapper cartMapper;

    @Autowired
    private ItemMapper itemMapper;

    public void insertCategory(Category category) {
        categoryMapper.insertCategory(category);
    }
    public void insertProduct(Product product) {
        productMapper.insertProduct(product);
    }


    //----商品展示模块----//
    public List<Category> getCategoryList() {
        return categoryMapper.getCategoryList();
    }

    public Category getCategory(String categoryId) {
        return categoryMapper.getCategory(categoryId);
    }

    public void deleteCategoryById(String categoryId) {
        categoryMapper.deleteCategory(categoryId);
    }

    public void editCategory(Category category) {
        categoryMapper.editCategory(category);
    }

    public Product getProduct(String productId) {
        return productMapper.getProduct(productId);
    }

    //----product----//
    public List<Product> getProductListByCategory(String categoryId) {
        return productMapper.getProductListByCategory(categoryId);
    }

    public void deleteProduct(String productId) {
        productMapper.deleteProduct(productId);
    }

    public void deleteProductByCategoryId(String categoryId) {
        productMapper.deleteProductByCategoryId(categoryId);
    }

    //----item----//
    public List<Item> getItemListByProduct(String productId) {
        return itemMapper.getItemListByProduct(productId);
    }

    public void updateItem(Item item) {
        itemMapper.updateItem(item);
    }

    public void addItem(Item item) {
        itemMapper.addItem(item);
    }

    public void addInventory(Item item) {
        itemMapper.addInventory(item);
    }

    public Item getItem(String itemId) {
        return itemMapper.getItem(itemId);
    }

    public void deleteItem(Item item) {
        itemMapper.deleteItem(item);
    }

    public void deleteItemByProductId(String productId) {
        itemMapper.deleteItemByProductId(productId);
    }

    public void updateInventory(Item item) {
        itemMapper.updateInventory(item);
    }

    public boolean isItemInStock(String itemId) {
        return itemMapper.getInventoryQuantity(itemId) > 0;
    }
    //----搜索----//
    public List<Product> searchProductList(String keyword) {
        return productMapper.searchProductList("%" + keyword.toLowerCase() + "%");
    }

    public List<Item> searchItemList(String keyword) {
        return itemMapper.searchItemList("%" + keyword.toLowerCase() + "%");
    }

    public List<Category> searchCategoryList(String keyword) {
        return categoryMapper.searchCategoryList("%" + keyword.toLowerCase() + "%");
    }

    //----购物车模块----//
    public void insertCartItem(Account account, Cart cart) {
        List<CartItem> cartItemList = cart.getCartItemList();
        for(int i=0;i<cartItemList.size();i++){
            cartMapper.updateCart(account,cartItemList.get(i),cartItemList.get(i).getQuantity(),cartItemList.get(i).getTotal());
        }
    }

    public Cart getCartByUsername(String username) {
        List<CartItem> cartItemList = cartMapper.getCartByUsername(username);
        Cart cart = new Cart();
        for (int i=0;i<cartItemList.size();i++){
            cart.addItem(cartItemList.get(i).getItem(),true);
        }
        return cart;
    }

    public void removeByUsername(Account account) {
        cartMapper.removeByUsername(account);
    }

    public void ChangeItemQuantity(String itemId,String username,String quantity,String total) {
        cartMapper.ChangeItemQuantity(itemId,username,quantity,total);
    }

    public void removeByUsernameAndItemId(Account account, String itemId) {
        cartMapper.removeByUsernameAndItemId(account, itemId);
    }

    public List<CartItem> getQuantityAndTotalByUsername(String username){
        List<CartItem> cartItemList = cartMapper.getQuantityAndTotalByUsername(username);
        return cartItemList;
    }
}